package com.tg.app.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.tg.app.R;

public class CloudAdView extends LinearLayout {
    private TextView  adContentText;
    private String url;

    public void setUrl(String url) {
        this.url = url;
    }
    private OnAdClickListener adClickListener;

    public void setAdClickListener(OnAdClickListener adClickListener) {
        this.adClickListener = adClickListener;
    }

    public CloudAdView(Context context) {
        super(context);
        intContext(context);
    }

    public CloudAdView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        intContext(context);
    }

    public CloudAdView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intContext(context);
    }

    private void  intContext(Context context){
      View view = LayoutInflater.from(context).inflate(R.layout.layout_cloud_ad_view,this,true);
      adContentText = view.findViewById(R.id.cloud_ad_content);
      view.findViewById(R.id.cloud_ad_closed).setOnClickListener(v -> {
          setVisibility(GONE);
          if (adClickListener!=null){
              adClickListener.onAdCloseBtnClick();
          }
      });
        adContentText.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(url)&&adClickListener!=null){
                adClickListener.onAdContentClick(url);
            }
        });
    }

    @Override
    public void setVisibility(int visibility) {
        if (TextUtils.isEmpty(adContentText.getText().toString())){
            visibility = GONE;
        }
        super.setVisibility(visibility);
    }

    public void setContentText(String content){
        adContentText.setText(content);
    }
    public  interface OnAdClickListener{
        void  onAdContentClick(String url);
        void  onAdCloseBtnClick();
    }

}
